package cn.itcast.crawler.test;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import sun.awt.geom.AreaOp;

import java.nio.charset.Charset;

public class CrawlerFirst {

    public static void main(String[] args) throws Exception {
        //1.打开浏览器，创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //2.输入网址，创建发起get请求创建HttpGet对象
        HttpGet httpGet = new HttpGet( "http://www.itcast.cn");
        //HttpGet httpGet = new HttpGet( "https://www.ithome.com");
        //3.按回车键，发起请求，返回响应，使用HttpClient对象发起请求
        CloseableHttpResponse response = httpClient.execute(httpGet);


        //4.解析响应，获取数据
        //判断状态码是否符合要求（这个itcast网站对应的状态码是200）
        if(response.getStatusLine().getStatusCode() == 200){
            HttpEntity httpEntity = response.getEntity();
            //获取静态页面
            String content = EntityUtils.toString(httpEntity, "utf-8");
            System.out.println(content);
        }

    }
}
